#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;

template<class K>
//struct BinarySearchTree
struct BSTreeNode
{
	BSTreeNode<K>* left;
	BSTreeNode<K>* right;
	K _key;

	BSTreeNode(const K& key)
		:left(nullptr)
		,right(nullptr)
		,_key(key)
	{

	}
};

template<class K>
struct BSTree
{
	typedef BSTreeNode<K> Node;
public:
	bool Insert(const K& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->right;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(key);
		if (key > parent->_key)
		{
			parent->right = cur;
		}
		else
		{
			parent->left = cur;
		}
		return true;
	}
	void InOrder()
	{
		_InOrder(_root);
	}

private:
	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;
		_InOrder(root->left);
		cout << root->_key << " ";
		_InOrder(root->right);
	}

private:
	Node* _root = nullptr;
};

void TestBinaryTree()
{
	int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
	BSTree<int> t1;

	for (auto e : a)
	{
		t1.Insert(e);
	}
	t1.InOrder();
}

int main()
{
	TestBinaryTree();

	return 0;
}